package com.zy.cat.dao.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

import com.github.pagehelper.Page;
import com.zy.cat.entity.resp.Pages;
import com.zy.cat.entity.resp.ResultPage;


@Repository
public class DaoImpl<T> extends SqlSessionDaoSupport implements com.zy.cat.dao.Dao<T> {
 
	@Resource
	@Override
	public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
		super.setSqlSessionFactory(sqlSessionFactory);
	}
	@Override
	public boolean insert(Object obj) {
		return this.getSqlSession().insert(getOName(obj) + ".insert", obj) > 0;
	}

	@Override
	public boolean insert(Object obj, String table) {
		return this.getSqlSession().insert(getOName(obj) + "." + table, obj) > 0;
	}

	@Override

	public boolean insert(Map<String, Object> search) {
		return this.getSqlSession().insert(search.get("table") + ".insert", search) > 0;
	}

	@Override

	public boolean insert(Map<String, Object> search, String table) {
		return this.getSqlSession().insert(search.get("table") + "." + table, search) > 0;
	}
	
	@Override
	public boolean insertBatch(List<T> obj,String table) {
		return this.getSqlSession().insert(getOName(obj.get(0)) + "." + table, obj) == obj.size();
	}

	@Override

	public boolean delete(Object obj) {
		return this.getSqlSession().delete(getOName(obj) + ".delete", obj) > 0;
	}

	@Override

	public boolean delete(Object obj, String table) {
		return this.getSqlSession().delete(getOName(obj) + "." + table, obj) > 0;
	}

	@Override

	public boolean delete(Map<String, Object> search) {
		return this.getSqlSession().delete(search.get("table") + ".delete", search) > 0;
	}

	@Override

	public boolean delete(Map<String, Object> search, String table) {
		return this.getSqlSession().delete(search.get("table") + "." + table, search) > 0;
	}

	@Override

	public boolean update(Object obj) {
		return this.getSqlSession().update(getOName(obj) + ".update", obj) > 0;
	}

	@Override

	public boolean update(Object obj, String table) {
		return this.getSqlSession().update(getOName(obj) + "." + table, obj) > 0;

	}

	@Override

	public boolean update(Map<String, Object> search) {
		return this.getSqlSession().update(search.get("table") + ".update", search) > 0;
	}

	@Override

	public boolean update(Map<String, Object> search, String table) {
		return this.getSqlSession().update(search.get("table") + "." + table, search) > 0;
	}
	
	@Override
	public boolean update(List<T> list, String table) {
		return this.getSqlSession().update(getOName(list.get(0)) + "." + table, list) == list.size();

	}

	@Override

	public List<T> list(Object obj) {
		return this.getSqlSession().selectList(getOName(obj) + ".list", obj);
	}

	@Override

	public List<T> list(Object obj, String table) {
		return this.getSqlSession().selectList(getOName(obj) + "." + table, obj);
	}

	@Override

	public List<T> list(Map<String, Object> search) {
		return this.getSqlSession().selectList(search.get("table") + ".list", search);
	}

	@Override

	public List<T> list(Map<String, Object> search, String table) {
		return this.getSqlSession().selectList(search.get("table") + "." + table, search);
	}

	@Override

	public T selectOne(Object obj) {
		return this.getSqlSession().selectOne(getOName(obj) + ".selectOne", obj);
	}

	@Override

	public T selectOne(Object obj, String table) {
		return this.getSqlSession().selectOne(getOName(obj) + "." + table, obj);
	}

	@Override

	public T selectOne(Map<String, Object> search) {
		return this.getSqlSession().selectOne(search.get("table") + ".selectOne", search);
	}

	@Override

	public T selectOne(Map<String, Object> search, String table) {
		return this.getSqlSession().selectOne(search.get("table") + "." + table, search);
	}
	@Override
	public ResultPage<T> listAndPages(Object obj, Pages pages) {
		@SuppressWarnings("unchecked")
		Page<T> list = (Page<T>) this.getSqlSession().selectList(getOName(obj) + ".listAndPages", obj,
				new RowBounds(pages.getPageNum(), pages.getPageSize()));
		if (list == null || list.isEmpty()) {
			return null;
		}
		return new ResultPage<T>(list);

	}

	@Override
	public ResultPage<T> listAndPages(Object obj, String table, Pages pages) {
		@SuppressWarnings("unchecked")
		Page<T> list = (Page<T>) this.getSqlSession().selectList(getOName(obj) + "."+table, obj,
				new RowBounds(pages.getPageNum(), pages.getPageSize()));
		if (list == null || list.isEmpty()) {
			return null;
		}
		return new ResultPage<T>(list);
	}

	@Override
	public ResultPage<T> listAndPages(Map<String, Object> search, String table, Pages pages) {
		@SuppressWarnings("unchecked")
		Page<T> list = (Page<T>) this.getSqlSession().selectList(search.get("table")  + "."+table, search,
				new RowBounds(pages.getPageNum(), pages.getPageSize()));
		if (list == null || list.isEmpty()) {
			return null;
		}
		return new ResultPage<T>(list);
	}

	@Override

	public ResultPage<T> listAndPages(Map<String, Object> search, Pages pages) {
		@SuppressWarnings("unchecked")
		Page<T> list = (Page<T>) this.getSqlSession().selectList(search.get("table")  + ".listAndPages", search,
				new RowBounds(pages.getPageNum(), pages.getPageSize()));
		if (list == null || list.isEmpty()) {
			return null;
		}
		return new ResultPage<T>(list);
	}
//	@Override
//
//	public Map<String, Object> listAndPages(Object obj, Pages pages) {
//		List<Object> rlist = new ArrayList<Object>();
//		Map<String, Object> map = new HashMap<String, Object>();
//		List<Object> list = this.getSqlSession().selectList(getOName(obj) + ".listAndPages", obj,
//				new RowBounds(pages.getPageNum(), pages.getPageSize()));
//		if (list == null || list.size() <= 0) {
//			return null;
//		}
//		for (Object object : list) {
//			rlist.add(object);
//		}
//		
//		Page<Object> page = (Page<Object>) list;
//		Pages rpages = new Pages(page.getPageNum(), page.getPageSize(), page.getStartRow(), page.getEndRow(),
//				page.getTotal(), page.getPages());
//		map.put("data", rlist);
//		map.put("pages", rpages);
//		return map;
//	}
//
//	@Override
//	public Map<String, Object> listAndPages(Object obj, String table, Pages pages) {
//		Map<String, Object> map = new HashMap<String, Object>();
//		List<Object> rlist = new ArrayList<Object>();
//		List<Object> list = this.getSqlSession().selectList(getOName(obj) + "." + table, obj,
//				new RowBounds(pages.getPageNum(), pages.getPageSize()));
//
//		if (list == null || list.size() <= 0) {
//			return null;
//		}
//		for (Object object : list) {
//			rlist.add(object);
//		}
//		Page<Object> page = (Page<Object>) list;
//		Pages rpages = new Pages(page.getPageNum(), page.getPageSize(), page.getStartRow(), page.getEndRow(),
//				page.getTotal(), page.getPages());
//		map.put("data", rlist);
//		map.put("pages", rpages);
//		return map;
//	}
//	@Override
//
//	public Map<String, Object> listAndPages(Map<String, Object> search, String table, Pages pages) {
//		Map<String, Object> map = new HashMap<String, Object>();
//		List<Object> rlist = new ArrayList<Object>();
//		List<Object> list = this.getSqlSession().selectList(search.get("table") + "." + table, search,
//				new RowBounds(pages.getPageNum(), pages.getPageSize()));
//		if (list == null || list.size() <= 0) {
//			return null;
//		}
//		for (Object object : list) {
//			rlist.add(object);
//		}
//		Page<Object> page = (Page<Object>) list;
//		Pages rpages = new Pages(page.getPageNum(), page.getPageSize(), page.getStartRow(), page.getEndRow(),
//				page.getTotal(), page.getPages());
//		map.put("data", rlist);
//		map.put("pages", rpages);
//		return map;
//	}
//
//	@Override
//
//	public Map<String, Object> listAndPages(Map<String, Object> search, Pages pages) {
//		Map<String, Object> map = new HashMap<String, Object>();
//		List<Object> rlist = new ArrayList<Object>();
//		List<Object> list = this.getSqlSession().selectList(search.get("table") + ".listAndPages", search,
//				new RowBounds(pages.getPageNum(), pages.getPageSize()));
//		if (list == null || list.size() <= 0) {
//			return null;
//		}
//		for (Object object : list) {
//			rlist.add(object);
//		}
//		Page<Object> page = (Page<Object>) list;
//		Pages rpages = new Pages(page.getPageNum(), page.getPageSize(), page.getStartRow(), page.getEndRow(),
//				page.getTotal(), page.getPages());
//		map.put("data", rlist);
//		map.put("pages", rpages);
//		return map;
//	}

	private String getOName(Object obj) {
		String str = obj.getClass().getName();
		return str.substring(str.lastIndexOf(".") + 1);
	}

	@Override
	public List<T> list(Object obj, Pages pages) {
		List<T> list = this.getSqlSession().selectList(getOName(obj) + ".list", obj,
				new RowBounds(pages.getPageNum(), pages.getPageSize()));
		if (list == null || list.size() <= 0) {
			return null;
		}
		return list;
	}

	@Override
	public List<T> list(Object obj, String table, Pages pages) {
		List<T> list = this.getSqlSession().selectList(getOName(obj) + "." + table, obj,
				new RowBounds(pages.getPageNum(), pages.getPageSize()));
		if (list == null || list.size() <= 0) {
			return null;
		}
		return list;
	}

	@Override
	public List<T> list(Map<String, Object> search, Pages pages) {
		List<T> list = this.getSqlSession().selectList(search.get("table") + ".list", search,
				new RowBounds(pages.getPageNum(), pages.getPageSize()));
		if (list == null || list.size() <= 0) {
			return null;
		}
		return list;
	}

	@Override
	public List<T> list(Map<String, Object> search, String table, Pages pages) {
		List<T> list = this.getSqlSession().selectList(search.get("table") + "." + table, search,
				new RowBounds(pages.getPageNum(), pages.getPageSize()));
		if (list == null || list.size() <= 0) {
			return null;
		}
		return list;
	}
	
	@Override
	public void callProcedure(Object obj, String table) {
	 this.getSqlSession().selectOne(getOName(obj) + "."+table, obj);
	}
}
